<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="考试编码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="examinationCode">
              <a-input v-model="model.examinationCode" placeholder="请输入考试编码"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="考试名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="examinationName">
              <a-input v-model="model.examinationName" placeholder="请输入考试名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="考试类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="examinationTtype">
              <j-dict-select-tag type="list" v-model="model.examinationTtype"
                                 dictCode="examination_ttype" placeholder="请选择考试类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="年级" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="gradeId">
              <a-select v-model="model.gradeId" placeholder="请选择考试年级"
                        :getPopupContainer="(target) => target.parentNode">
                <a-select-option v-for="item in gradeDataList" :key="item.value">{{ item.label }}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="学年" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="schoolYearId">
              <a-select v-model="model.schoolYearId" placeholder="请选择考试学年"
                        :getPopupContainer="(target) => target.parentNode">
                <a-select-option v-for="item in schoolYearDataList" :key="item.value">{{ item.label }}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="学期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="semesterId">
              <a-select v-model="model.semesterId" placeholder="请选择考试学期"
                        :getPopupContainer="(target) => target.parentNode">
                <a-select-option v-for="item in semesterDataList" :key="item.value">{{ item.label }}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="所属部门" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sysOrgCode">
<!--              <j-dict-select-tag type="list" v-model="model.sysOrgCode" dictCode="" placeholder="请选择所属部门编码" />-->
              <a-select v-model="model.sysOrgCode" placeholder="请选择所属部门"
                        :getPopupContainer="(target) => target.parentNode">
                <a-select-option v-for="item in departDataList" :key="item.value">{{ item.label }}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="是否启用" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="izOpen">
              <j-dict-select-tag type="list" v-model="model.izOpen = 1"
                                 dictCode="iz_open" placeholder="请选择是否启用(是否启用（0停用 1启用）)" />
            </a-form-model-item>
          </a-col>
<!--          <a-col :span="24">-->
<!--            <a-form-model-item label="是否删除" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="delFlag">-->
<!--              <j-dict-select-tag type="list" v-model="model.delFlag = 0"-->
<!--                                 dictCode="del_flag" placeholder="选择是否删除(0正常未删除，1已删除)" />-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
<!--          <a-col :span="24">-->
<!--            <a-form-model-item label="创建人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="createBy">-->
<!--              <a-input v-model="model.createBy" placeholder="请输入创建人"  ></a-input>-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
<!--          <a-col :span="24">-->
<!--            <a-form-model-item label="修改人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="updateBy">-->
<!--              <a-input v-model="model.updateBy" placeholder="请输入修改人"  ></a-input>-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { getAllGrade, getSchoolYearDataList, getSemesterDataList, getDepartDataList} from '@api/commonAPI'

  export default {
    name: 'AchExaminationForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: "/achexamination/achExamination/add",
          edit: "/achexamination/achExamination/edit",
          queryById: "/achexamination/achExamination/queryById"
        },
        gradeDataList: [],
        schoolYearDataList: [],
        semesterDataList: [],
        departDataList: [],
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
      this.getGradeData();
      this.getSchoolYearData();
      this.getSemesterData();
      this.getDepartData();
    },
    methods: {
      add () {
        //考虑自动当前用户id赋给创建人及修改人
        // var loginUserData= window.localStorage.getItem("pro__Login_Userinfo");//嵌套值id获取
        // loginUserData=JSON.parse(loginUserData);
        // this.model.createBy = loginUserData.value.id
        this.modelDefault.izOpen = 1, //是否启用（0停用 1启用）
        this.modelDefault.delFlag = 0, //是否删除（0正常未删除，1已删除）
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
      async getSchoolYearData() {
        let res = await getSchoolYearDataList()
        this.schoolYearDataList = []
        res.result.forEach(item => {
          this.schoolYearDataList.push({
            label: item.schoolYearName,
            value: item.id
          })
        })
      },
      async getSemesterData() {
        let res = await getSemesterDataList()
        this.semesterDataList = []
        res.result.forEach(item => {
          this.semesterDataList.push({
            label: item.semesterName,
            value: item.id
          })
        })
      },
      async getGradeData() {
        let res = await getAllGrade()
        this.gradeDataList = []
        res.result.forEach(item => {
          this.gradeDataList.push({
            label: item.gradeName,
            value: item.id
          })
        })
      },
      async getDepartData() {
        let res = await getDepartDataList()
        this.departDataList = []
        res.result.forEach(item => {
          this.departDataList.push({
            label: item.departName,
            value: item.orgCode
          })
        })
      },

    }
  }
</script>